﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace HPKaraoke
{
    /// <summary>
    /// Interaction logic for SearchForm.xaml
    /// </summary>
    public partial class SearchForm : UserControl
    {
        public SearchForm()
        {
            InitializeComponent();
        }

        List<SongInfo> _source = null;
        public Window WindowParent { get; set; }

        public static readonly RoutedEvent SearchEndedEvent = EventManager.RegisterRoutedEvent(
        "SearchEnded", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SearchForm));

        public static readonly RoutedEvent OnDoubleClickSongInfoEvent = EventManager.RegisterRoutedEvent(
            "DoubleClickItem", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SearchForm));

        public event RoutedEventHandler OnSearchEnded
        {
            add { AddHandler(SearchEndedEvent, value); }
            remove { RemoveHandler(SearchEndedEvent, value); }
        }

        public event RoutedEventHandler OnDoubleClickItem
        {
            add { AddHandler(OnDoubleClickSongInfoEvent, value); }
            remove { RemoveHandler(OnDoubleClickSongInfoEvent, value); }
        }

        void RaiseSearchEndedEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(SearchForm.SearchEndedEvent);
            RaiseEvent(newEventArgs);
        }

        void RaiseDoubleClickItemEvent(SongInfo songInfo)
        {
            RoutedEventArgs arg = new RoutedEventSongInfoDoubleClickArg(SearchForm.OnDoubleClickSongInfoEvent);
            ((RoutedEventSongInfoDoubleClickArg)arg).Value = songInfo;
            RaiseEvent(arg);
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            _source = LibraryController.Find(tbKeyword.Text);
            dgResult.ItemsSource =  _source;
        }

        private void dgResult_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //search the object hierarchy for a datagrid row
            DependencyObject source = (DependencyObject)e.OriginalSource;
            var row = UIHelper.TryFindParent<DataGridRow>(source);

            //the user did not click on a row
            if (row == null) return;

            this.Tag = _source[row.GetIndex()].ID;

            RaiseDoubleClickItemEvent(_source[row.GetIndex()]);



            e.Handled = true;
        }

        private void userControl_Loaded(object sender, RoutedEventArgs e)
        {
            tbKeyword.Text = "";
            tbKeyword.Focus();
        }

        private void tbKeyword_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (tbKeyword.Text == "Type keyword to find")
                tbKeyword.Text = "";
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            RaiseSearchEndedEvent();
        }

        private void tbKeyword_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbKeyword.Text.Trim()))
            {
                _source = LibraryController.Find(tbKeyword.Text);
                if (dgResult != null)
                    dgResult.ItemsSource = _source;
                Storyboard sbShowResult = (Storyboard)FindResource("ShowResult");
                sbShowResult.Begin();
            }
        }
    }
}
